package com.sdp.right.control;

import java.util.Map;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.service.IService;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.utils.RunnerUtil;


@RestController
@RequestMapping("/SysApplicationController")
public class SysApplicationController {

	@Autowired
	private Query query;
	
	@Autowired
	private RunnerUtil runnerUtil;
	
	@SuppressWarnings("unchecked")
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) throws Exception {
		if(StringUtils.isEmpty(dataCenter.getEntityClassName())) {
			return Result.error("实体信息不存在");
		}
		Map<String,Object> serviceMap = runnerUtil.getServiceMap();
		if(serviceMap==null || serviceMap.size()==0) {
			return Result.error("对应的service不存在");
		}
		String entityName = dataCenter.getEntityClassName().substring(dataCenter.getEntityClassName().lastIndexOf(".")+1);
		if(!serviceMap.containsKey(entityName)) {
			return Result.error("对应的service不存在");
		}

		Class<?> clazz = Class.forName(dataCenter.getEntityClassName());
		IService<T> serviceImpl = (IService<T>) serviceMap.get(entityName);
		return Result.ok(query.page(serviceImpl, clazz, dataCenter));
	}
	
	
	@SuppressWarnings("unchecked")
	@PostMapping(value = "/list")
	public Result<?> list(@RequestBody DataCenter dataCenter) throws Exception {
		if(StringUtils.isEmpty(dataCenter.getEntityClassName())) {
			return Result.error("实体信息不存在");
		}
		Map<String,Object> serviceMap = runnerUtil.getServiceMap();
		if(serviceMap==null || serviceMap.size()==0) {
			return Result.error("对应的service不存在");
		}
		String entityName = dataCenter.getEntityClassName().substring(dataCenter.getEntityClassName().lastIndexOf(".")+1);
		if(!serviceMap.containsKey(entityName)) {
			return Result.error("对应的service不存在");
		}

		Class<?> clazz = Class.forName(dataCenter.getEntityClassName());
		IService<T> serviceImpl = (IService<T>) serviceMap.get(entityName);
		return Result.ok(query.list(serviceImpl, clazz, dataCenter));
	}
	
	@SuppressWarnings("unchecked")
	@PostMapping(value = "/save")
	public Result<?> save(@RequestBody DataCenter dataCenter) throws Exception {
		if(StringUtils.isEmpty(dataCenter.getData())) {
			return Result.error("数据不存在");
		}
		if(StringUtils.isEmpty(dataCenter.getEntityClassName())) {
			return Result.error("实体信息不存在");
		}
		Map<String,Object> serviceMap = runnerUtil.getServiceMap();
		if(serviceMap==null || serviceMap.size()==0) {
			return Result.error("对应的service不存在");
		}
		String entityName = dataCenter.getEntityClassName().substring(dataCenter.getEntityClassName().lastIndexOf(".")+1);
		if(!serviceMap.containsKey(entityName)) {
			return Result.error("对应的service不存在");
		}
		IService<T> serviceImpl = (IService<T>) serviceMap.get(entityName);
		serviceImpl.save((T) dataCenter.getData());
		return Result.ok();
	}
	
	
}
